package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.position.EmployeeNoListRequest;
import com.mes.cloud.api.dto.request.position.SysPositionOrgAuthRequest;
import com.mes.cloud.api.dto.request.position.SysPositionOrganizationAuthPageRequest;
import com.mes.cloud.api.dto.request.position.SysPositionOrganizationAuthRequest;
import com.mes.cloud.api.dto.response.employee.SysEmployeePositionDto;
import com.mes.cloud.api.dto.response.position.SysPositionOrganizationAuthDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysOrganizationDao;
import com.mes.cloud.dao.SysPositionOrganizationAuthDao;
import com.mes.cloud.entity.SysPositionLog;
import com.mes.cloud.entity.SysPositionOrganizationAuth;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位组织授权关联表
 *
 * @author xy
 * @version 2019-08-02 14:16:11
 * @email yuan.xue3@pactera.com
 */
@Service
public class SysPositionOrganizationAuthService {

    private SysPositionOrganizationAuthDao sysPositionOrganizationAuthDao;

    private IPKeyGenerator ipKeyGenerator;

    private SysPositionChannelTypeService sysPositionChannelTypeService;

    private LogService logService;

    private SysEmployeePositionService sysEmployeePositionService;

    private SysOrganizationDao sysOrganizationDao;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public SysPositionOrganizationAuthService(SysPositionOrganizationAuthDao sysPositionOrganizationAuthDao
            , IPKeyGenerator ipKeyGenerator
            , SysPositionChannelTypeService sysPositionChannelTypeService
            , LogService logService
            , SysEmployeePositionService sysEmployeePositionService, SysOrganizationDao sysOrganizationDao) {
        this.sysPositionOrganizationAuthDao = sysPositionOrganizationAuthDao;
        this.ipKeyGenerator = ipKeyGenerator;
        this.sysPositionChannelTypeService = sysPositionChannelTypeService;
        this.logService = logService;
        this.sysEmployeePositionService = sysEmployeePositionService;
        this.sysOrganizationDao = sysOrganizationDao;
    }

    /**
     * <分页查列表>
     *
     * @param pageRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.tntframe.core.common.model.CommPager < com.shinho.crm.base.api.dto.response.position.SysPositionOrganizationAuthDto>>
     * @author xy
     * @date 2019/8/8 18:01
     */
    public JsonResult<CommPager<SysPositionOrganizationAuthDto>> pageList(SysPositionOrganizationAuthPageRequest pageRequest) {

        Page<SysPositionOrganizationAuthDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        SysPositionOrganizationAuth selectParam = new SysPositionOrganizationAuth();
        BeanUtils.copyProperties(pageRequest, selectParam);
        List<SysPositionOrganizationAuth> sysPositionOrganizationAuthList = sysPositionOrganizationAuthDao.select(selectParam);

        List<SysPositionOrganizationAuthDto> sysPositionOrganizationAuthDtoList = new ArrayList<>();
        for (SysPositionOrganizationAuth sysPositionOrganizationAuth : sysPositionOrganizationAuthList) {
            SysPositionOrganizationAuthDto sysPositionOrganizationAuthDto = new SysPositionOrganizationAuthDto();
            BeanUtils.copyProperties(sysPositionOrganizationAuth, sysPositionOrganizationAuthDto);
            sysPositionOrganizationAuthDtoList.add(sysPositionOrganizationAuthDto);
        }

        CommPager<SysPositionOrganizationAuthDto> pageResult = new CommPager<>(page, sysPositionOrganizationAuthDtoList);
        JsonResult<CommPager<SysPositionOrganizationAuthDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);

        return jsonResult;
    }

    /**
     * <查列表>
     *
     * @param sysPositionOrganizationAuthRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List < com.shinho.crm.base.api.dto.response.position.SysPositionOrganizationAuthDto>>
     * @author xy
     * @date 2019/8/8 18:02
     */
    public JsonResult<List<SysPositionOrganizationAuthDto>> list(SysPositionOrganizationAuthRequest sysPositionOrganizationAuthRequest) {

        //查询条件
        SysPositionOrganizationAuth selectParam = new SysPositionOrganizationAuth();
        BeanUtils.copyProperties(sysPositionOrganizationAuthRequest, selectParam);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysPositionOrganizationAuth> sysPositionOrganizationAuthList = sysPositionOrganizationAuthDao.select(selectParam);

        List<SysPositionOrganizationAuthDto> sysPositionOrganizationAuthDtoList = new ArrayList<>();
        for (SysPositionOrganizationAuth sysPositionOrganizationAuth : sysPositionOrganizationAuthList) {
            SysPositionOrganizationAuthDto sysPositionOrganizationAuthDto = new SysPositionOrganizationAuthDto();
            BeanUtils.copyProperties(sysPositionOrganizationAuth, sysPositionOrganizationAuthDto);
            sysPositionOrganizationAuthDtoList.add(sysPositionOrganizationAuthDto);
        }

        JsonResult<List<SysPositionOrganizationAuthDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysPositionOrganizationAuthDtoList);

        return jsonResult;
    }

    /**
     * <组织授权>
     *
     * @param sysPositionOrgAuthRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author xy
     * @date 2019/8/8 15:20
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult organizationAuth(SysPositionOrgAuthRequest sysPositionOrgAuthRequest) {

        String positionId = sysPositionOrgAuthRequest.getPositionId();
        //查询岗位是否已经有数据
        SysPositionOrganizationAuth queryParams = new SysPositionOrganizationAuth();
        queryParams.setPositionId(positionId);
        queryParams.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysPositionOrganizationAuth> list = sysPositionOrganizationAuthDao.select(queryParams);
        List<String> organiztionIds = sysPositionOrgAuthRequest.getOrganiztionIds();
        //如果已有数据 或者本次提交数据胃口 更新岗位组织授权数据状态为不可用
        if (!CollectionUtils.isEmpty(organiztionIds) || !CollectionUtils.isEmpty(list)) {
            this.updateByPositionId(sysPositionOrgAuthRequest);
        }
        //如果提交组织数据不为空
        if (!CollectionUtils.isEmpty(organiztionIds) && organiztionIds.size() > 0) {
            List<SysPositionOrganizationAuth> spoa = new ArrayList<>();
            for (String organizationId : organiztionIds) {
                SysPositionOrganizationAuth sysPositionOrganizationAuth = new SysPositionOrganizationAuth();
                sysPositionOrganizationAuth.setId(ipKeyGenerator.getGenerateKey());
                sysPositionOrganizationAuth.setPositionId(positionId);
                sysPositionOrganizationAuth.setOrganizationId(organizationId);
                sysPositionOrganizationAuth.setUpdateDateStr(DateUtil.updateDateStr());
                sysPositionOrganizationAuth.setCreateBy(UserUtil.getCreateByName());
                sysPositionOrganizationAuth.setUpdateBy(UserUtil.getUpdateByName());
                sysPositionOrganizationAuth.setCreateByNo(UserUtil.getCreateByNo());
                sysPositionOrganizationAuth.setUpdateByNo(UserUtil.getUpdateByNo());
                spoa.add(sysPositionOrganizationAuth);
            }
            sysPositionOrganizationAuthDao.batchInsert(spoa);
        }
        this.insertPositionLog(positionId, Constants.UPDATE_AUTH_ORGANIZATION);
        sysPositionChannelTypeService.batchInsert(sysPositionOrgAuthRequest);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        return jsonResult;
    }

    private void updateByPositionId(SysPositionOrgAuthRequest sysPositionOrgAuthRequest) {
        sysPositionOrgAuthRequest.setPositionId(sysPositionOrgAuthRequest.getPositionId());
        sysPositionOrgAuthRequest.setUpdateBy(UserUtil.getUpdateByName());
        sysPositionOrgAuthRequest.setUpdateByNo(UserUtil.getUpdateByNo());
        sysPositionOrgAuthRequest.setUpdateDateStr(DateUtil.updateDateStr());
        sysPositionOrgAuthRequest.setDelFlag(Constants.DEL_FLAG_YES);
        sysPositionOrganizationAuthDao.updateByPositionId(sysPositionOrgAuthRequest);
    }

    /**
     * <插入岗位日志>
     *
     * @param positionId, logMessage
     * @return int
     * @author xy
     * @date 2019/8/12 8:38
     */
    @SuppressWarnings("Duplicates")
    private void insertPositionLog(String positionId, String logMessage) {
        SysPositionLog sysPositionLog = new SysPositionLog();
        sysPositionLog.setPositionId(positionId);
        sysPositionLog.setMenuCode(Constants.POSITION_MENU_CODE);
        sysPositionLog.setLog(logMessage);
        sysPositionLog.setId(ipKeyGenerator.getGenerateKey());
        sysPositionLog.setUpdateDateStr(DateUtil.updateDateStr());
        sysPositionLog.setCreateBy(UserUtil.getCreateByName());
        sysPositionLog.setUpdateBy(UserUtil.getUpdateByName());
        sysPositionLog.setCreateByNo(UserUtil.getCreateByNo());
        sysPositionLog.setUpdateByNo(UserUtil.getUpdateByNo());
        logService.insertPositionLog(sysPositionLog);
    }

    /**
     * <获取当前登录者组织>
     *
     * @return List<String>
     * @author xy
     * @date 2019/9/10 8:33
     */
    public JsonResult<List<String>> getOrganizationIdList() {
        JsonResult<List<SysEmployeePositionDto>> epList = sysEmployeePositionService.getPositionIdList();
        List<SysEmployeePositionDto> sysEmployeePositionDtos = epList.getData();
        List<String> positionIds = new ArrayList<>();
        for (SysEmployeePositionDto sysEmployeePositionDto : sysEmployeePositionDtos) {
            positionIds.add(sysEmployeePositionDto.getPositionId());
        }
        List<String> organizationIds = sysPositionOrganizationAuthDao.getOrganizationIdList(positionIds);
        JsonResult<List<String>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(organizationIds);
        return jsonResult;
    }

    /**
     * 描述 获取当前登录人，组织下的所有员工号
     *
     * @param employeeNoListRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List < java.lang.String>>
     * @author 郑样平
     * @date 2019/10/24 15:22
     */
    public JsonResult<List<String>> getEmployeeNoList(EmployeeNoListRequest employeeNoListRequest) {
        //当前登录人的授权组织
        List<String> employeeNoList = new ArrayList<>();
        List<String> organizationIds = sysPositionOrganizationAuthDao.getOrganizationIdList(employeeNoListRequest.getPositionIds());
        if (!CollectionUtils.isEmpty(organizationIds)) {
            //查询所有下级组织
            List<String> listChildren = sysOrganizationDao.listChildren(organizationIds);
            employeeNoList = sysPositionOrganizationAuthDao.getEmployeeNoList(listChildren);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(employeeNoList);
        return jsonResult;
    }
}
